package com.yiren.dbaa.module.device.model;

import android.app.Activity;
import android.app.AlertDialog;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.yiren.backstage.config.api.LoadConfigApi;
import com.yiren.backstage.config.bean.device.CarSignalConfigBean;
import com.yiren.backstage.currency.enums.CarSignalEnum;
import com.yiren.dbaa.R;
import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.module.device.contract.DeviceConfigContract;
import com.yiren.dbaa.module.param_config.api.ParamConfigApi;
import com.yiren.dbaa.net.bean.WiringConfigBean;
import com.yiren.dbaa.util.DensityUtil;
import com.yiren.dbaa.view.adapter.WiringSignalAdapter;

import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author lwb
 * @date : 2022/7/14 15:35
 * @description: 设备配置M
 */
public class DeviceConfigModel implements DeviceConfigContract.Model {
    private Activity activity;

    List<CarSignalEnum> signalLists; //信号list
    private List<WiringConfigBean> wiringConfigList;
    private Map<String, String> map = new HashMap<String, String>();
    private Gson gson;

    private Map<String, Object> signalMap;//车辆信号map

    public DeviceConfigModel(Activity activity) {
        this.activity = activity;
        gson = new Gson();
    }

    @Override
    public void setWiringConfig() {
        // 判断有没有配置过的信号，有则不显示
        String pwd = LoadConfigApi.loadDeviceStudyConfig(activity).getJxConfig();
        wiringConfigList = new ArrayList<>();
        if (StringUtils.isNotEmpty(pwd)) {
            map = gson.fromJson(pwd, map.getClass());
            Set<String> lineNames = map.keySet();

        }

    }

    @Override
    public List<CarSignalEnum> setDialogContent(Map<String, String> map) {
        // 得到需要添加的信号
        CarSignalEnum[] carSignalEnums = CarSignalEnum.values();
        signalLists = new ArrayList<CarSignalEnum>();
        for (int i = 0; i < carSignalEnums.length; i++) {
            if (!signalLists.contains(carSignalEnums[i])) {
                signalLists.add(carSignalEnums[i]);
            }
        }
        // 判断有没有配置过的信号，有则不显示
        String pwd = LoadConfigApi.loadDeviceStudyConfig(activity).getJxConfig();
        if (null != pwd && !"".equals(pwd)) {
            map = gson.fromJson(pwd, map.getClass());
            Iterator it = map.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry entry = (Map.Entry) it.next();
                Object key = entry.getKey();
                Object value = entry.getValue();
                for (int i = 0; i < signalLists.size(); i++) {
                    if (value.equals(signalLists.get(i).getCode())) {
                        signalLists.remove(i);
                        break;
                    }
                }
            }
        }
        return signalLists;
    }

    @Override
    public void showSelectSignalDialog(CommonCallBack callBack, List<WiringConfigBean> wiringConfigList, int linePosition) {
        AlertDialog dialog = new AlertDialog.Builder(activity).create();
        dialog.show();
        // 高度占了全屏，临时给了一个高度，代码未优化
        WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
        params.width = (DensityUtil.getScreenWidth(activity) * 6) / 10;
        params.height = (DensityUtil.getScreenHeight(activity) * 6) / 10;
        dialog.getWindow().setAttributes(params);
        Window window = dialog.getWindow();
        window.setContentView(R.layout.dialog_select_box);
        ListView lvSignalSelect = (ListView) window.findViewById(R.id.lv_dialog_list);
        lvSignalSelect.setDivider(null);// 去除listview的下划线
        WiringSignalAdapter wiringSignalAdapter = new WiringSignalAdapter(activity, signalLists);
        lvSignalSelect.setAdapter(wiringSignalAdapter);
        TextView tv_title = (TextView) window.findViewById(R.id.tv_title);
        tv_title.setText("选择“" + wiringConfigList.get(linePosition).getLineName() + "”车辆信号");
        LinearLayout ll_button = (LinearLayout) window.findViewById(R.id.ll_button);
        ll_button.setVisibility(View.GONE);
        lvSignalSelect.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                callBack.success(position);
                dialog.dismiss();
            }
        });
    }

    @Override
    public void setCarStateSignal(String carstateName, CommonCallBack callBack) {
        CarSignalConfigBean carSignalConfig = LoadConfigApi.loadCarSignalConfig(activity);
        signalMap = carSignalConfig.getMap();
        boolean isSignal = (boolean) signalMap.get(carstateName);
        signalMap.put(carstateName, !isSignal);
        carSignalConfig.setMap(signalMap);
        ParamConfigApi.saveCarSignalConfig(activity, carSignalConfig);
        callBack.success("");
    }

}